package com.example.downloader;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.downloader.adapter.DownloadAdapter;
import com.example.downloader.adapter.UploadAdapter;
import com.example.downloader.model.BaseDownloader;
import com.example.downloader.model.DownloadCallback;
import com.example.downloader.model.DownloadInfo;
import com.example.downloader.model.DownloadTask;
import com.example.downloader.model.ProgressHolder;
import com.example.downloader.upload.MyUploader;
import com.example.downloader.upload.TaskExecutor;
import com.example.downloader.upload.UploadProgress;

import java.util.ArrayList;
import java.util.List;

import static com.example.downloader.model.DownloadCallback.FLAG_CANCLED;
import static com.example.downloader.model.DownloadCallback.FLAG_DOWNLOADING;
import static com.example.downloader.model.DownloadCallback.FLAG_ERROR;
import static com.example.downloader.model.DownloadCallback.FLAG_FINISHED;
import static com.example.downloader.model.DownloadCallback.FLAG_FREE;
import static com.example.downloader.model.DownloadCallback.FLAG_READY;

public class MainActivity extends BaseActivity {

    private ListView mListView;

    private MyUploader mUploader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) findViewById(R.id.listview);
        mUploader = new MyUploader(new TaskExecutor<String>() {
            @Override
            public String onCompressing(String path) throws Throwable {
                Thread.sleep(1000);
                return "";
            }

            @Override
            public String onUploading(String compressedPath, UploadProgress progress) throws Throwable {
                for (int i = 0; i < 100; ++i) {
                    Thread.sleep(100);
                    progress.setUploadProgress(i);
                }


                return "1";
            }
        });
        findViewById(R.id.cancel_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUploader.cancelAll();
            }
        });

        findViewById(R.id.okhttp).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OKHttpDownloadActivity.gotoActivity(MainActivity.this);
            }
        });

        List<DownloadInfo> infos = new ArrayList<>();
        for (int i = 0; i < 100; ++i) {
            infos.add(new DownloadInfo("http://" + String.valueOf(i), "图片" + String.valueOf(i)));
        }


        UploadAdapter adapter = new UploadAdapter(this, infos, mUploader);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                final DownloadInfo info = (DownloadInfo) parent.getAdapter().getItem(position);
                mUploader.cancel(info.uri);

            }
        });
    }

    private static class SimpleDownloader<Void> extends BaseDownloader<Void> {
        public SimpleDownloader(String downloadPath) {
            super(downloadPath);
        }

        @Override
        protected DownloadTask<Void> makeDownloadTask(String filePath, String uri, DownloadCallback<Void> callback) {
            return new SimpleDownloadTask<>(callback);
        }
    }

    private static class SimpleDownloadTask<Void> extends DownloadTask<Void> {
        public SimpleDownloadTask(DownloadCallback callbck) {
            super(callbck);
        }

        @Override
        protected Void doInBackground2(String... params) throws Throwable {
            for (int i = 0; i < 100; ++i) {
                if (isCancelled()) {
                    return null;
                }

                Thread.sleep(50);
                publishProgress(PROGRESS, 100, i + 1);
            }
            return null;
        }
    }

}
